<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式锁解决方案 | Redis宕机应对策略</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1f2937;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .solution-icon {
            font-size: 2.5rem;
            color: #3b82f6;
            margin-bottom: 1rem;
        }
        .feature-badge {
            background-color: #e0f2fe;
            color: #0369a1;
        }
        .footer-link:hover {
            color: #93c5fd;
        }
        .mermaid-container {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="bg-white shadow-sm py-4">
        <div class="container mx-auto px-6 flex justify-between items-center">
            <a href="#" class="text-xl font-bold text-gray-800">
                <i class="fas fa-lock-open mr-2 text-blue-500"></i>分布式锁专家
            </a>
            <div class="hidden md:flex space-x-8">
                <a href="#" class="text-gray-600 hover:text-blue-500">解决方案</a>
                <a href="#" class="text-gray-600 hover:text-blue-500">最佳实践</a>
                <a href="#" class="text-gray-600 hover:text-blue-500">案例分析</a>
                <a href="#" class="text-gray-600 hover:text-blue-500">关于我们</a>
            </div>
            <button class="md:hidden text-gray-600">
                <i class="fas fa-bars"></i>
            </button>
        </div>
    </nav>

    <!-- Hero 区域 -->
    <section class="hero text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">Redis宕机时的分布式锁解决方案</h1>
            <p class="text-xl md:text-2xl max-w-3xl mx-auto mb-8">
                当线程成功获取到分布式锁，但此时Redis宕机时，如何确保系统的稳定性和可靠性？
            </p>
            <div class="flex flex-wrap justify-center gap-4">
                <a href="#solutions" class="bg-white text-blue-600 px-6 py-3 rounded-full font-medium hover:bg-blue-50 transition">
                    <i class="fas fa-lightbulb mr-2"></i>查看解决方案
                </a>
                <a href="#contact" class="border border-white text-white px-6 py-3 rounded-full font-medium hover:bg-white hover:text-blue-600 transition">
                    <i class="fas fa-envelope mr-2"></i>联系我们
                </a>
            </div>
        </div>
    </section>

    <!-- 主要内容区域 -->
    <main class="container mx-auto px-6 py-12">
        <!-- 简介部分 -->
        <section class="mb-16">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <h2 class="text-2xl font-bold mb-4 text-gray-800">分布式锁的关键挑战</h2>
                <p class="text-gray-700 mb-6">
                    在分布式系统中，Redis被广泛用作分布式锁的实现方案。但当Redis宕机时，已经获取的锁可能无法正常释放，导致系统出现死锁或性能下降。合理的设计和实施分布式锁机制，配合监控和容错机制，可以有效应对Redis宕机对系统造成的影响。
                </p>
                <div class="mermaid-container">
                    <div class="mermaid">
                        flowchart TD
                            A[客户端获取锁成功] --> B{Redis宕机?}
                            B -->|是| C[锁自动过期释放]
                            B -->|否| D[正常业务处理]
                            C --> E[系统恢复]
                            D --> F[释放锁]
                            E --> G[重新获取锁]
                    </div>
                </div>
            </div>
        </section>

        <!-- 解决方案部分 -->
        <section id="solutions" class="mb-16">
            <h2 class="text-3xl font-bold text-center mb-12 text-gray-800">六种解决方案</h2>
            
            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
                <!-- 方案1 -->
                <div class="card bg-white rounded-xl overflow-hidden">
                    <div class="p-6">
                        <div class="solution-icon">
                            <i class="fas fa-clock"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">锁的自动过期时间</h3>
                        <p class="text-gray-600 mb-4">
                            在获取锁时设置适当的锁过期时间，即使Redis宕机，锁也会在一定时间后自动释放。这样虽然不能立即释放锁，但可以保证最终一致性。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">可靠性</span>
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">简单实现</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 p-4 border-t border-gray-200">
                        <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1706433946317-d3058cc7-1086-449f-833c-4566bfa198dd.png" alt="锁的自动过期时间" class="w-full rounded">
                    </div>
                </div>
                
                <!-- 方案2 -->
                <div class="card bg-white rounded-xl overflow-hidden">
                    <div class="p-6">
                        <div class="solution-icon">
                            <i class="fas fa-heartbeat"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">Redis健康监控</h3>
                        <p class="text-gray-600 mb-4">
                            设置监控机制定期检测Redis的健康状态，及时发现宕机情况并进行处理。可以结合告警系统实现快速响应。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">实时性</span>
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">主动防御</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 p-4 border-t border-gray-200">
                        <img src="https://cdn.nlark.com/yuque/0/2024/webp/21449790/1706433991241-36d222c9-07ec-460e-8755-e4fc0aa93177.webp" alt="Redis健康监控" class="w-full rounded">
                    </div>
                </div>
                
                <!-- 方案3 -->
                <div class="card bg-white rounded-xl overflow-hidden">
                    <div class="p-6">
                        <div class="solution-icon">
                            <i class="fas fa-cubes"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">分布式锁框架</h3>
                        <p class="text-gray-600 mb-4">
                            借助Redisson、Curator等成熟框架，它们提供了更稳定的分布式锁实现，能够自动处理Redis宕机等异常情况。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">稳定性</span>
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">社区支持</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 p-4 border-t border-gray-200">
                        <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1706604136590-0be39928-0850-43e5-9c74-994ab625ca13.png" alt="分布式锁框架" class="w-full rounded">
                    </div>
                </div>
                
                <!-- 方案4 -->
                <div class="card bg-white rounded-xl overflow-hidden">
                    <div class="p-6">
                        <div class="solution-icon">
                            <i class="fas fa-redo"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">锁的重入机制</h3>
                        <p class="text-gray-600 mb-4">
                            如果线程获取锁后发现Redis宕机，可以尝试重新获取锁或等待后重试，直到成功为止。确保关键业务不被中断。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">容错性</span>
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">高可用</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 p-4 border-t border-gray-200">
                        <div class="mermaid">
                            sequenceDiagram
                                participant C as Client
                                participant R as Redis
                                C->>R: 获取锁
                                R-->>C: 成功
                                Note over R: Redis宕机
                                loop 重试机制
                                    C->>R: 重新获取锁
                                    R-->>C: 成功(恢复后)
                                end
                        </div>
                    </div>
                </div>
                
                <!-- 方案5 -->
                <div class="card bg-white rounded-xl overflow-hidden">
                    <div class="p-6">
                        <div class="solution-icon">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">备用锁机制</h3>
                        <p class="text-gray-600 mb-4">
                            设计备用的锁实现，如基于数据库或ZooKeeper的分布式锁，当Redis宕机时自动切换，保证系统正常运行。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">高可用</span>
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">灾备方案</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 p-4 border-t border-gray-200">
                        <div class="mermaid">
                            graph LR
                                A[主锁: Redis] -->|宕机| B[备用锁: ZooKeeper]
                                A -->|正常| C[业务处理]
                                B --> C
                        </div>
                    </div>
                </div>
                
                <!-- 方案6 -->
                <div class="card bg-white rounded-xl overflow-hidden">
                    <div class="p-6">
                        <div class="solution-icon">
                            <i class="fas fa-heart"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">心跳检测机制</h3>
                        <p class="text-gray-600 mb-4">
                            使用心跳检测监控Redis状态，发现宕机立即通知业务模块采取容错策略，如降级处理或告警人工介入。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">实时监控</span>
                            <span class="feature-badge text-sm px-3 py-1 rounded-full">快速响应</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 p-4 border-t border-gray-200">
                        <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1706604064926-f3950de1-d7c8-4f1b-99ce-30ea2efbf85a.png" alt="心跳检测机制" class="w-full rounded">
                    </div>
                </div>
            </div>
        </section>

        <!-- 总结部分 -->
        <section class="bg-blue-50 rounded-xl p-8 mb-16">
            <h2 class="text-2xl font-bold mb-6 text-gray-800">方案总结与选择建议</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-lg font-semibold mb-3 text-gray-700">实施优先级</h3>
                    <ol class="list-decimal list-inside space-y-2 text-gray-700">
                        <li class="pl-2">首先设置锁的自动过期时间 - 基础保障</li>
                        <li class="pl-2">实现Redis健康监控 - 主动发现问题</li>
                        <li class="pl-2">使用成熟框架 - 减少自研风险</li>
                        <li class="pl-2">添加重入机制 - 提高容错能力</li>
                        <li class="pl-2">考虑备用方案 - 关键业务保障</li>
                        <li class="pl-2">完善心跳检测 - 精细化监控</li>
                    </ol>
                </div>
                <div>
                    <h3 class="text-lg font-semibold mb-3 text-gray-700">适用场景</h3>
                    <ul class="list-disc list-inside space-y-2 text-gray-700">
                        <li class="pl-2">高并发系统: 优先考虑框架+过期时间+重入</li>
                        <li class="pl-2">金融交易系统: 必须实现备用锁机制</li>
                        <li class="pl-2">中小型应用: 监控+过期时间通常足够</li>
                        <li class="pl-2">关键业务系统: 建议所有方案组合使用</li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- 页脚 -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto px-6">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                    <p class="text-gray-400">专业的分布式系统解决方案</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="footer-link text-gray-300 hover:text-blue-300 transition">
                        <i class="fas fa-globe mr-2"></i>技术小馆地址: http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-gray-400 text-sm">
                &copy; 2024 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        // 初始化Mermaid图表
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>